<?php

use api\admin\mq;

require_once $_SERVER["DOCUMENT_ROOT"]."/global/data.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/global/function.php";
class AesUtil
{
    private $aesKey;

    const KEY_LENGTH_BYTE = 32;
    const AUTH_TAG_LENGTH_BYTE = 16;

    public function __construct($aesKey)
    {
        if (strlen($aesKey) != self::KEY_LENGTH_BYTE) {
            throw new InvalidArgumentException('无效的ApiV3Key，长度应为32个字节');
        }
        $this->aesKey = $aesKey;
    }

    public function decryptToString($associatedData, $nonceStr, $ciphertext)
    {
        $ciphertext = base64_decode($ciphertext);
        if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {
            return false;
        }

        if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) {
            return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);
        }

        if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) {
            return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);
        }

        if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', openssl_get_cipher_methods())) {
            $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
            $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);

            return openssl_decrypt($ctext, 'aes-256-gcm', $this->aesKey, OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
        }

        throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
    }
}
//支付结果回调处理
$data = json_decode(file_get_contents("php://input"), true);
$data =json_decode( (new AesUtil($GLOBALS["wechat_pay_key"]))->decryptToString($data["resource"]["associated_data"], $data["resource"]["nonce"], $data["resource"]["ciphertext"]),true);
$mchid=$data["mchid"];
$appid=$data["appid"];
$out_trade_no=$data["out_trade_no"];
$transaction_id=$data["transaction_id"];
$trade_type=$data["trade_type"];
$trade_state=$data["trade_state"];
$trade_state_desc=$data["trade_state_desc"];
$bank_type=$data["bank_type"];
$success_time=$data["success_time"];
$openid=$data["payer"]["openid"];
$total=$data["amount"]["total"];
$time=time();
$con=con();
$res=mysqli_query($con,"insert into pay_log(mchid,appid,out_trade_no,transaction_id,trade_type,trade_state,trade_state_desc,bank_type,success_time,openid,total) values('$mchid','$appid','$out_trade_no','$transaction_id','$trade_type','$trade_state','$trade_state_desc','$bank_type','$success_time','$openid','$total')");
$res=mysqli_query($con,"update `order` set status=1,pay_time=$time,update_time=$time where order_no='$out_trade_no'");
//打印结账单
$order_id=mysqli_query($con,"select * from `order` where order_no='$out_trade_no'");
$order_id=mysqli_fetch_all($order_id,true);
if(count($order_id)!==0){
    $order_id=(int)$order_id[0]['id'];
    $res=(new mq())->printOrder($order_id);
}
mysqli_close($con);

